from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from fdfs_client.client import Fdfs_client
from django.conf import settings

# jwt验证返回数据
def jwt_response_payload_handler(token, user=None, request=None):
    return {
        'token': token,
        'username': user.username,
        'id': user.id
    }


# 分页查询
class MyNumbPage(PageNumberPagination):
    page_size = 5
    page_size_query_param = 'pagesize'
    max_page_size = 5

    def get_paginated_response(self, data):
        return Response({
            "lists": data,
            'page': self.page.number,
            'pages': self.page.paginator.num_pages
        })


def upload_image(img):
    # 校验参数
    if not img:
        return None
    client = Fdfs_client(settings.FDFS_CONF)
    ret = client.upload_appender_by_buffer(img.read())
    if ret["Status"] != "Upload successed.":
        return None
    # 图片入库（fdfs）
    img_url = ret['Remote file_id']
    return img_url
